﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GraphicsFramework.Drawing.SizingHandles
{
    class SizingHandle : GraphicBase
    {

        private List<SizingNode> nodes;

        public SizingHandle()
            : base(Enum.EnumDrawingType.POLYGON, "Sizing Handles", Enum.EnumMouseClicks.NONE)
        {
            properties = Property.DefaultProperty.SelectedUnscaled;
            properties.ShowFill = false;

            nodes = new List<SizingNode>();
            for (int count = 1; count <= 8; count ++)
                nodes.Add(new SizingNode());

        }

        public void Initialize(GraphicsFramework graphics, float x1, float y1, float x2, float y2)
        {
            if (x1 > x2)
            {
                float temp = x1;
                x1 = x2;
                x2 = temp;
            }

            if (y1 > y2)
            {
                float temp = y1;
                y1 = y2;
                y2 = temp;
            }

            points = new List<Entity.Point2F>();

            points.Add(new Entity.Point2F(x1, y1));
            points.Add(new Entity.Point2F(x1, y2));
            points.Add(new Entity.Point2F(x2, y2));
            points.Add(new Entity.Point2F(x2, y1));
        }

        public override void OnRender(GraphicsFramework graphics)
        {
            float size = 2 / (float) graphics.Gridsize;
            float common;
            int count = 0;

            //Edge nodes
            foreach (Entity.Point2F point in points)
            {
                nodes[count] = new SizingNode(point.X - size, point.Y - size, point.X + size, point.Y + size);
                count ++;
            }

            //Left node
            common = (points[0].Y + points[1].Y) / 2;
            nodes[4] = new SizingNode(points[0].X - size, common - size, points[0].X + size, common + size);

            //Right node
            nodes[6] = new SizingNode(points[2].X - size, common - size, points[2].X + size, common + size);

            //Top node
            common = (points[1].X + points[2].X) / 2;
            nodes[5] = new SizingNode(common - size, points[1].Y - size, common + size, points[1].Y + size);

            //Bottom node
            nodes[7] = new SizingNode(common - size, points[3].Y - size, common + size, points[3].Y + size);

            //Draw the nodes
            foreach (SizingNode node in nodes)
                node.OnRender(graphics);

            //Draw the boundary
            graphics.DrawPolygon(properties, points);
        }
    }
}
